package com.gravata.enigma;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

import com.gravata.enigma.activities.TelaInicial;
import com.gravata.enigma.adapter.ComboBouquetAdapter;
import com.gravata.enigma.adapter.SateliteComboAdapter;
import com.gravata.enigma.adapter.TransponderComboAdapter;
import com.gravata.enigma.model.Satelite;
import com.gravata.enigma.model.Transponder;

public class RefreshData extends AsyncTask<String, String, Boolean> {
	private Context context;
	private ProgressDialog progress;
	private ReloadDataListener dataListener;
	public RefreshData(Context context,ReloadDataListener dataListener) {
		this.context=context;
		this.dataListener=dataListener;
	}

	@Override
	protected Boolean doInBackground(String... params) {

		TelaInicial.reloadData();

		TelaInicial.comboBouquetAdapter = new ComboBouquetAdapter(
				(Activity) context, TelaInicial.bouquets);
		if (TelaInicial.profile != null)
			TelaInicial.sateliteComboAdapter = new SateliteComboAdapter(
					(Activity) context,
					TelaInicial.repositorioSatelite
							.listar(TelaInicial.profile));
		else {
			List<Satelite> tmps = new ArrayList<Satelite>();
			TelaInicial.sateliteComboAdapter = new SateliteComboAdapter(
					(Activity) context, tmps);
		}

		List<Transponder> tmp = new ArrayList<Transponder>();
		TelaInicial.transponderComboAdapter = new TransponderComboAdapter(
				(Activity) context, tmp);

		TelaInicial.transponderComboAdapter.notifyDataSetChanged();
		TelaInicial.sateliteComboAdapter.notifyDataSetChanged();

		dataListener.onReloadData();

		return true;
	}

	@Override
	protected void onPreExecute() {
		 progress = new ProgressDialog((Activity) context);
		 progress.setMessage(context.getString(R.string.carregando)+"..."); progress.setCancelable(false);
		 progress.show();

	}

	@Override
	protected void onProgressUpdate(String... values) {
		// Atualiza mensagem
		if (progress != null)
			progress.setMessage(values[0]);
	}

	@Override
	protected void onPostExecute(Boolean result) {
		if (!result) {
			try {
				Thread.sleep(4000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		progress.dismiss();
	}

}
